package com.business_clean.ui.activity.camera;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.ext.CommonUtils;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.data.mode.camera.LeaderClockEntity;
import com.business_clean.data.mode.camera.LeaderClockListEntity;
import com.business_clean.data.mode.camera.LeaderClockUserListEntity;
import com.business_clean.data.mode.roster.RosterList;
import com.business_clean.databinding.ActivityLeaderClockBinding;
import com.business_clean.ui.activity.personnel.SelectPersonnelActivity;
import com.business_clean.ui.adapter.camera.LeaderItemAdapter;
import com.business_clean.viewmodel.request.CameraViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.lxj.xpopup.interfaces.OnSelectListener;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import me.hgj.mvvmhelper.ext.AppExtKt;

public class LeaderClockActivity extends BaseActivity<CameraViewModel, ActivityLeaderClockBinding> {

    //        private LeaderAdapter mAdapter; //这是原先的写法，想把列表去做处理
    private LeaderItemAdapter mAdapter;
    private LeaderItemAdapter mAdapter1;

    private String class_uuid0;
    private String class_uuid1;

    private String class_time;
    private String class_time1;
    private String class_name1;
    private String class_name;

    private String uuids;

    private boolean isTopAdapter = true;//记录点击那个 adapter 然后 add 到某个里面

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        View footerView = View.inflate(this, R.layout.item_leader_clock_footview, null);
        //因为只有 2 个，先写死，后期优化
        mAdapter = new LeaderItemAdapter();
        mDatabind.recycler.setLayoutManager(new LinearLayoutManager(this));
        mDatabind.recycler.setAdapter(mAdapter);
        mAdapter.addFooterView(footerView);

        //1
        mAdapter1 = new LeaderItemAdapter();
        mDatabind.recycler1.setLayoutManager(new LinearLayoutManager(this));
        mDatabind.recycler1.setAdapter(mAdapter1);
        View footerView1 = View.inflate(this, R.layout.item_leader_clock_footview, null);
        mAdapter1.addFooterView(footerView1);

        //根据传递过来的参数
        if (getIntent() != null && getIntent().getExtras() != null) {
            LeaderClockEntity leaderEntity = (LeaderClockEntity) getIntent().getExtras().getSerializable("leader_clock");
            if (leaderEntity != null) {
                fillData(leaderEntity);
            } else {
                //请求数据
                mViewModel.requestAttendance();
            }
        }
    }


    @Override
    public void initObserver() {
        //选择人员 回调，
        App.appViewModelInstance.getSelectedPersonnel().observe(this, new Observer<List<RosterList>>() {
            @Override
            public void onChanged(List<RosterList> rosterLists) {
                StringBuilder builderUUIDs = new StringBuilder();

                //选择好人员后，更新当前的班次列表
                for (RosterList item : rosterLists) {
                    if (item.isChoice()) {
                        builderUUIDs.append(item.getUuid()).append(",");

                        LeaderClockUserListEntity entity = new LeaderClockUserListEntity();
                        entity.setAvatar(item.getAvatar());
                        entity.setUser_name(item.getUser_name());
                        entity.setStatus_name("加班");
                        entity.setStatus("6");
                        entity.setUuid(item.getUuid());
                        entity.setJob_name(item.getJob_name());
                        entity.setProject_name(entity.getProject_name());
                        entity.setAdd(true);
                        if (isTopAdapter) {
                            mAdapter.getData().add(mAdapter.getData().size(), entity);
                        } else {
                            mAdapter1.getData().add(mAdapter1.getData().size(), entity);
                        }
                    }
                }
                if (isTopAdapter) {
                    mAdapter.notifyDataSetChanged();
                } else {
                    mAdapter1.notifyDataSetChanged();
                }

                if (builderUUIDs.length() > 0) {
                    builderUUIDs.deleteCharAt(builderUUIDs.length() - 1);
                }

                uuids = builderUUIDs.toString();
            }
        });
    }

    @Override
    public void onBindViewClick() {
        //这里是保存内容到上个领班打卡的内容
        mDatabind.bottom.butBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存到公共的，然后传递给上层 如果有多个组合起来
                LeaderClockEntity leaderClock = new LeaderClockEntity();
                List<LeaderClockListEntity> listEntities = new ArrayList<>();
                //班次 1
                LeaderClockListEntity leaderClockListEntity = new LeaderClockListEntity();
                leaderClockListEntity.setClass_name(class_name);
                leaderClockListEntity.setClass_time(class_time);
                leaderClockListEntity.setUuid(class_uuid0);

                leaderClockListEntity.setUser_list(mAdapter.getData());
                //班次 2
                LeaderClockListEntity leaderClockListEntity1 = new LeaderClockListEntity();
                leaderClockListEntity1.setClass_name(class_name1);
                leaderClockListEntity1.setClass_time(class_time1);
                leaderClockListEntity1.setUuid(class_uuid1);
                leaderClockListEntity1.setUser_list(mAdapter1.getData());
                //增加到 list
                listEntities.add(leaderClockListEntity);
                listEntities.add(leaderClockListEntity1);
                //内容
                leaderClock.setClass_list(listEntities);
                App.appViewModelInstance.getLeaderData().setValue(leaderClock);
                AppExtKt.finishActivity(LeaderClockActivity.class);
            }
        });

        //底部增加人员
        mAdapter.getFooterLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isTopAdapter = true;
                Bundle bundle = new Bundle();
                bundle.putInt("multiple", 2);
                bundle.putInt("channel", 1);
                bundle.putString("class_uuid", "" + class_uuid0);
                bundle.putString("uuids", "" + class_uuid0);
                ActivityForwardUtil.startActivity(SelectPersonnelActivity.class, bundle);
            }
        });
        //底部增加人员
        mAdapter1.getFooterLayout().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isTopAdapter = false;
                Bundle bundle = new Bundle();
                bundle.putInt("multiple", 2);
                bundle.putInt("channel", 1);
                bundle.putString("class_uuid", "" + class_uuid0);
                ActivityForwardUtil.startActivity(SelectPersonnelActivity.class, bundle);
            }
        });


        //这里是 item 改动
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.tv_item_leader_item_jiaban) {//如果是加班的，不删除，但是隐藏掉状态修改
                    mAdapter.getData().remove(position);//删除某一个
                    mAdapter.notifyDataSetChanged();
                } else if (view.getId() == R.id.tv_item_leader_item_status) {
                    if (!mAdapter.getData().get(position).isAdd()) {//只有不是新增的才可以修改数据
                        showStatusDialog(mAdapter.getData().get(position), mAdapter);
                    }
                }
            }
        });


        //这里是 item 改动
        mAdapter1.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.tv_item_leader_item_jiaban) {//如果是加班的，不删除，但是隐藏掉状态修改
                    mAdapter1.getData().remove(position);//删除某一个
                    mAdapter1.notifyDataSetChanged();
                } else if (view.getId() == R.id.tv_item_leader_item_status) {
                    if (!mAdapter1.getData().get(position).isAdd()) {//只有不是新增的才可以修改数据
                        showStatusDialog(mAdapter1.getData().get(position), mAdapter1);
                    }
                }
            }
        });
    }


    /**
     * 更新 item 的数据
     */
    private void showStatusDialog(LeaderClockUserListEntity data, LeaderItemAdapter leaderItemAdapter) {
        String[] strings = {"改为休假", "改为出勤", "改为旷工", "改为缺卡"};
        CommonUtils.showBottomListWith(this, 20, "请选择出勤情况", strings, new OnSelectListener() {
            @Override
            public void onSelect(int position, String text) {
                String status = "";//1休假 2正常出勤  4旷工 5休息 6加班
                String statusName = "";
                switch (position) {
                    case 0:
                        status = "1";
                        statusName = "休假";
                        break;
                    case 1:
                        status = "2";
                        statusName = "出勤";
                        break;
                    case 2:
                        status = "4";
                        statusName = "旷工";
                        break;
                    case 3:
                        status = "3";
                        statusName = "缺卡";
                        break;
                }
                data.setStatus(status);
                data.setStatus_name(statusName);
                if (leaderItemAdapter != null) {
                    leaderItemAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onRequestSuccess() {
        mViewModel.getLeaderClockEntity().observe(this, new Observer<LeaderClockEntity>() {
            @Override
            public void onChanged(LeaderClockEntity leaderClockEntity) {
                fillData(leaderClockEntity);
            }
        });
    }


    public void fillData(LeaderClockEntity leaderClockEntity) {
        if (leaderClockEntity.getClass_list().size() >= 1) {
            mDatabind.tvClassName0.setVisibility(View.VISIBLE);
            mDatabind.tvClassName0.setText(leaderClockEntity.getClass_list().get(0).getClass_name());
            class_uuid0 = leaderClockEntity.getClass_list().get(0).getUuid();
            class_name = leaderClockEntity.getClass_list().get(0).getClass_name();
            class_time = leaderClockEntity.getClass_list().get(0).getClass_time();
            mAdapter.setList(leaderClockEntity.getClass_list().get(0).getUser_list());
        }
        if (leaderClockEntity.getClass_list().size() >= 2) {
            mDatabind.tvClassName1.setVisibility(View.VISIBLE);
            mDatabind.recycler1.setVisibility(View.VISIBLE);
            mDatabind.tvClassName1.setText(leaderClockEntity.getClass_list().get(1).getClass_name());
            class_uuid1 = leaderClockEntity.getClass_list().get(1).getUuid();
            class_name1 = leaderClockEntity.getClass_list().get(1).getClass_name();
            class_time1 = leaderClockEntity.getClass_list().get(1).getClass_time();
            mAdapter1.setList(leaderClockEntity.getClass_list().get(1).getUser_list());
        }
        if (mAdapter.getData().size() != 0 || mAdapter1.getData().size() != 0) {
            mDatabind.leaderEmpty.llEmpty.setVisibility(View.GONE);
        }
    }
}